package com.ww.springboot.boot.thread.study.base.futuretask;

import org.apache.tomcat.util.threads.ThreadPoolExecutor;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.*;

/**
 * 描述：
 *
 * FutureTask 继承自Runnable 同时内含callable 可以实现异步执行获取返回值
 *
 * Callable 只有线程池才能使用 单个线程创建无法使用
 *
 *
 * @author 🧑 ‍wanwei
 * @since 2025-08-01 11:26
 */
public class FutureTaskDemo1 {

    private final static ThreadPoolExecutor executor = new ThreadPoolExecutor(0, 10, 5,
            TimeUnit.SECONDS, new LinkedBlockingQueue<>(1000));

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask<String> futureTask = new FutureTask(() -> "hello world");

        new Thread(futureTask).start();
        String s = futureTask.get();
        System.out.println(s);

        executor.execute(futureTask);
        System.out.println(futureTask.get());

        Future<String> submit = executor.submit(() -> "hello world");
        System.out.println(submit.get());

        List<Future<Object>> futures = executor.invokeAll(Arrays.asList(() -> "hello world", () -> "hello world"));
        futures.forEach(future -> {
            try {
                System.out.println(future.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        });
    }
}
